import java.awt.event.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.swing.*;

/**
 * AddEditTreatmentRecord
 * Panel with GUI to add or edit a TreatmentRecord
 *
 *
 *  Date           	Name            	Action
 *  --------------------------------------------------------------------------------------
 *	03/06/11		Bethany				Created Class
 *
 *
 *
 * @author bethanysumner
 * @version $Revision: 1.0 $
 */

public class AddEditTreatmentRecord extends JPanel {
	
	/**
	 * Field main.
	 */
	private MainWindow main;
	/**
	 * Field database.
	 */
	private Database database;
	/**
	 * Field appt.
	 */
	private String[] appt;
	/**
	 * Field treatment.
	 */
	private String[] treatment;
	/**
	 * Field time.
	 */
	/**
	 * Field date.
	 */
	private String date, time;
	
	
	/**
	 * Field comboYear.
	 */
	/**
	 * Field comboDay.
	 */
	/**
	 * Field comboMonth.
	 */
	private JComboBox comboMonth, comboDay, comboYear;
	/**
	 * Field comboDoctor.
	 */
	private JComboBox comboDoctor;
	/**
	 * Field comboNurse.
	 */
	private JComboBox comboNurse;
	/**
	 * Field txtBloodPressureS.
	 */
	/**
	 * Field txtBloodPressureD.
	 */
	/**
	 * Field txtPulse.
	 */
	/**
	 * Field txtTemp.
	 */
	/**
	 * Field txtHeight.
	 */
	/**
	 * Field txtWeight.
	 */
	private JTextField txtWeight, txtHeight, txtTemp, txtPulse, txtBloodPressureD, txtBloodPressureS; //vital signs
	/**
	 * Field txtSymptoms.
	 */
	private JTextField txtSymptoms;
	/**
	 * Field txtDiagnosis.
	 */
	private JTextField txtDiagnosis;
	/**
	 * Field btnSubmit.
	 */
	/**
	 * Field btnAddDrOrder.
	 */
	/**
	 * Field btnCancel.
	 */
	private JButton btnCancel, btnAddDrOrder, btnSubmit;
	/**
	 * Field buttonListener.
	 */
	private ButtonListener buttonListener;
	/**
	 * Field txtPatient.
	 */
	private JTextField txtPatient;
	
	
	/**
	 * AddEditTreatmentRecord Constructor - Creates GUI
	 * @param main A reference to the main window
	 * @param database A reference to the database
	
	 * @param date String
	 * @param time String
	 */
	public AddEditTreatmentRecord(MainWindow main, Database database, String date, String time) {
		
		this.main = main;
		this.database = database;
		
		
		try {
			treatment = database.getTreatment(date, time);
			createLayout();
			if(treatment[0] == null){ 
				//No treatment record exists for the appointment
				main.showMessage("No treatment record has been created. Please create a new one.");
				try {
					appt = database.getAppt(date, time);
				} catch (SQLException e) {
					main.showError("Error getting information from database.");
					e.printStackTrace();
				}
				init_createTR();
			}else{ 
				// A treatment record does exist for the appointment
				main.showMessage("Please edit the following information below.");
				init_editTR();
			}
		} catch (SQLException e) {
			main.showError("Error getting information from database.");
			e.printStackTrace();
		}
		
		
	}
	
	/**
	 * Adds the GUI elements to the panel
	 */
	private void createLayout(){
		// we're using absolute positioning
		setLayout(null);
		
		// patient field
		JLabel lblPatient = new JLabel("Patient:");
		lblPatient.setBounds(25, 56, 144, 16);
		add(lblPatient);
		
		// date field
		JLabel lblDate = new JLabel("Date:");
		lblDate.setBounds(25, 95, 148, 16);
		add(lblDate);
		comboMonth = new JComboBox();
		comboMonth.setBounds(188, 90, 110, 27);
		comboMonth.addItem("January");
		comboMonth.addItem("Febuary");
		comboMonth.addItem("March");
		comboMonth.addItem("April");
		comboMonth.addItem("May");
		comboMonth.addItem("June");
		comboMonth.addItem("July");
		comboMonth.addItem("August");
		comboMonth.addItem("September");
		comboMonth.addItem("October");
		comboMonth.addItem("November");
		comboMonth.addItem("December");
		add(comboMonth);
		JLabel slash1 = new JLabel("/");
		slash1.setBounds(310, 95, 14, 16);
		add(slash1);
		comboDay = new JComboBox();
		comboDay.setBounds(326, 90, 71, 27);
		for(int i = 1; i<=31; i++){
			if(i < 10){
				comboDay.addItem("0" + i);
			}else{
				comboDay.addItem("" + i);
			}
		}
		add(comboDay);
		JLabel slash2 = new JLabel("/");
		slash2.setBounds(409, 95, 14, 16);
		add(slash2);
		comboYear = new JComboBox();
		comboYear.setBounds(429, 90, 92, 27);
		for(int i = 0; i<=100; i++){
			comboYear.addItem("" + (2011 - i));
		}
		add(comboYear);
		
		// doctor field
		JLabel lblDoctor = new JLabel("Doctor:");
		lblDoctor.setBounds(25, 131, 61, 16);
		add(lblDoctor);
		try {
			comboDoctor = new JComboBox(database.getUsersOfType(1));
		} catch (SQLException e1) {
			main.showError("Error getting doctor information from database");
			e1.printStackTrace();
		}
		comboDoctor.setBounds(188, 126, 210, 27);
		add(comboDoctor);
		
		// nurse field
		JLabel lblNurse = new JLabel("Nurse");
		lblNurse.setBounds(25, 170, 61, 16);
		add(lblNurse);
		try {
			comboNurse = new JComboBox(database.getUsersOfType(2));
		} catch (SQLException e) {
			main.showError("Error getting nurse information from database");
			e.printStackTrace();
		}
		comboNurse.setBounds(188, 165, 210, 27);
		add(comboNurse);
		
		// doctor's orders field
		JLabel lblDoctorsOrders = new JLabel("Doctor's Orders:");
		lblDoctorsOrders.setBounds(25, 218, 148, 16);
		add(lblDoctorsOrders);
		
		// symptoms field
		JLabel lblSymptoms = new JLabel("Symptoms:");
		lblSymptoms.setBounds(25, 263, 148, 16);
		add(lblSymptoms);
		txtSymptoms = new JTextField();
		txtSymptoms.setColumns(10);
		txtSymptoms.setBounds(189, 263, 311, 27);
		add(txtSymptoms);
		
		// vital signs field
		JLabel lblVitalSigns = new JLabel("Vital Signs:");
		lblVitalSigns.setBounds(25, 325, 148, 16);
		add(lblVitalSigns);
		
		// diagnosis field
		JLabel lblDiagnosis = new JLabel("Diagnosis:");
		lblDiagnosis.setBounds(25, 467, 148, 16);
		add(lblDiagnosis);
		txtDiagnosis = new JTextField();
		txtDiagnosis.setColumns(10);
		txtDiagnosis.setBounds(189, 462, 311, 27);
		add(txtDiagnosis);
		
		
		//BUTTONS 
		
		buttonListener = new ButtonListener();
		
		btnAddDrOrder = new JButton("Doctor's Orders");
		btnAddDrOrder.setBounds(189, 216, 161, 21);
		btnAddDrOrder.addActionListener(buttonListener);
		add(btnAddDrOrder);
		
		btnSubmit = new JButton("Submit");
		btnSubmit.setBounds(366, 551, 134, 29);
		btnSubmit.addActionListener(buttonListener);
		add(btnSubmit);
		
		btnCancel = new JButton("Cancel");
		btnCancel.setBounds(218, 551, 134, 29);
		btnCancel.addActionListener(buttonListener);
		add(btnCancel);
		
		txtWeight = new JTextField();
		txtWeight.setText("0");
		txtWeight.setBounds(246, 323, 86, 20);
		add(txtWeight);
		txtWeight.setColumns(10);
		
		JLabel lblWeight = new JLabel("Weight:");
		lblWeight.setBounds(189, 326, 73, 14);
		add(lblWeight);
		
		txtHeight = new JTextField();
		txtHeight.setText("0");
		txtHeight.setBounds(246, 358, 86, 20);
		add(txtHeight);
		txtHeight.setColumns(10);
		
		JLabel lblHeight = new JLabel("Height:");
		lblHeight.setBounds(189, 361, 73, 14);
		add(lblHeight);
		
		JLabel lblTemp = new JLabel("Temp:");
		lblTemp.setBounds(367, 326, 61, 14);
		add(lblTemp);
		
		txtTemp = new JTextField();
		txtTemp.setText("0");
		txtTemp.setBounds(414, 323, 86, 20);
		add(txtTemp);
		txtTemp.setColumns(10);
		
		JLabel lblPulse = new JLabel("Pulse:");
		lblPulse.setBounds(367, 361, 61, 14);
		add(lblPulse);
		
		txtPulse = new JTextField();
		txtPulse.setText("0");
		txtPulse.setBounds(414, 358, 86, 20);
		add(txtPulse);
		txtPulse.setColumns(10);
		
		JLabel lblBloodPressure = new JLabel("Blood Pressure (D/S) :");
		lblBloodPressure.setBounds(189, 402, 145, 14);
		add(lblBloodPressure);
		
		txtBloodPressureD = new JTextField();
		txtBloodPressureD.setText("0");
		txtBloodPressureD.setBounds(332, 399, 73, 20);
		add(txtBloodPressureD);
		txtBloodPressureD.setColumns(10);
		
		txtBloodPressureS = new JTextField();
		txtBloodPressureS.setText("0");
		txtBloodPressureS.setColumns(10);
		txtBloodPressureS.setBounds(424, 399, 76, 20);
		add(txtBloodPressureS);
		
		JLabel label = new JLabel("/");
		label.setBounds(412, 401, 16, 16);
		add(label);
		
		txtPatient = new JTextField();
		txtPatient.setColumns(10);
		txtPatient.setBounds(187, 51, 210, 27);
		add(txtPatient);
		
	}
	
	/**
	 * Populates the fields based on the database values for treatmentID
	 */
	private void init_editTR(){
		
		int doctor = 0, nurse = 1, date = 2, time = 3, patientID = 4,
			symptoms = 8, bpd = 9, bps = 10, pulse = 11, temp = 12,
			weight = 14, diagnosis = 15;
		
		this.date = treatment[date];
		this.time = treatment[time];
		
		txtPatient.setText(treatment[patientID]);
		
		String[] dayMonthYear = treatment[date].split(" ");
		comboDay.setSelectedItem(dayMonthYear[0]);
		comboMonth.setSelectedItem(dayMonthYear[1]);
		comboYear.setSelectedItem(dayMonthYear[2]);
		
		comboDoctor.setSelectedItem(treatment[doctor]);
		comboNurse.setSelectedItem(treatment[nurse]);
		txtSymptoms.setText(treatment[symptoms]);
		txtWeight.setText(treatment[weight]);
		txtHeight.setText(treatment[weight]);
		txtTemp.setText(treatment[temp]);
		txtPulse.setText(treatment[pulse]);
		txtBloodPressureD.setText(treatment[bpd]);
		txtBloodPressureS.setText(treatment[bps]);
		txtDiagnosis.setText(treatment[diagnosis]);
		
		disableComponents();
	}
	
	/**
	 * Initializes the GUI to create a new treatment record
	 */
	private void init_createTR(){
		
		int date = 0, time = 1, patient = 2, doctor = 4;
		
		this.date = appt[date];
		this.time = appt[time];
		
		String[] dayMonthYear = appt[date].split(" ");
		
		txtPatient.setText(appt[patient]);
		comboDay.setSelectedItem(dayMonthYear[0]);
		comboMonth.setSelectedItem(dayMonthYear[1]);
		comboYear.setSelectedItem(dayMonthYear[2]);
		
		comboDoctor.setSelectedItem(appt[doctor]);
		
		disableComponents();
	}
	
	/**
	 * Disables components that should not be changed
	 */
	private void disableComponents(){
		
		comboDay.setEnabled(false);
		comboMonth.setEnabled(false);
		comboYear.setEnabled(false);
		txtPatient.setEnabled(false);
		if(main.getUsertype() != 1){
			btnAddDrOrder.setEnabled(false);
		}
		
	}
	
	/**
	 * Submit any changes to the database
	 */
	private void submit(){
		
		String patientID = txtPatient.getText();
		String doctor = comboDoctor.getSelectedItem().toString();
		String nurse = comboNurse.getSelectedItem().toString();
		String symptoms = txtSymptoms.getText();
		String diagnosis = txtDiagnosis.getText();
		
		float weight = Float.parseFloat(txtWeight.getText());
		int height = Integer.parseInt(txtHeight.getText());
		float temp = Float.parseFloat(txtTemp.getText());
		int pulse = Integer.parseInt(txtPulse.getText());
		int bloodPressureD = Integer.parseInt(txtBloodPressureD.getText());
		int bloodPressureS = Integer.parseInt(txtBloodPressureS.getText());
		
		
		if(treatment[0] == null){ 
			try {
				database.addTreatmentRecord(date, time);
				database.updateTR_basicInfo(date, time, patientID, doctor, nurse, symptoms, diagnosis);
				database.updateTR_vitalSigns(date, time, weight, height, temp, pulse, bloodPressureD, bloodPressureS);
				main.showAffirmation("Treatment record created successfully!");
				main.removeTab(this);
			} catch (SecurityException e) {
				main.showError("Error adding treatment record.");
				e.printStackTrace();
			} catch (SQLException e) {
				main.showError("Error adding treatment record.");
				e.printStackTrace();
			}
		}else{
			//call database to update treatment record
			try {
				database.updateTR_basicInfo(date, time, patientID, doctor, nurse, symptoms, diagnosis);
				database.updateTR_vitalSigns(date, time, weight, height, temp, pulse, bloodPressureD, bloodPressureS);
				main.showAffirmation("Treatment record updated successfully!");
				main.removeTab(this);
			} catch (SecurityException e) {
				main.showError("Error editing treatment record.");
				e.printStackTrace();
			} catch (SQLException e) {
				main.showError("Error editing treatment record.");
				e.printStackTrace();
			}
		}
	}
	
	/**
	 * Brings up the AddDrsOrders panel
	 */
	private void addDrOrders(){
		if(treatment[0] == null){ 
			try {
				database.addTreatmentRecord(date, time);
				treatment = database.getTreatment(date, time);
			} catch (SecurityException e) {
				main.showError("Error adding treatment record.");
				e.printStackTrace();
			} catch (SQLException e) {
				main.showError("Error adding treatment record.");
				e.printStackTrace();
			}
		}
		main.addTab("Doctor's Orders", new AddEditDoctorsOrders(main, database, date, time));
		main.clearMessage();
	}
	
	/**
	 * Cancels the changes and closes the tab
	 */
	private void cancel(){
		main.removeTab(this);
		main.clearMessage();
	}
	
	
	
	/**
	 * ButtonListener
	 * ActionListener to listen when button has been pressed
	 *
	 *
	 *  Date           	Name            	Action
	 *  --------------------------------------------------------------------------------------
	 *	03/06/11		Bethany				Created Class
	 *
	 *
	 *
	 * @author bethanysumner
	 */
	
	private class ButtonListener implements ActionListener{
		
		/**
		 * Performs tasks corresponding to the action
		 * @param event The action event
		
		 * @see java.awt.event.ActionListener#actionPerformed(ActionEvent) */
		public void actionPerformed(ActionEvent event){
			
			if(event.getSource().equals(btnSubmit)){
				submit();
			}else if(event.getSource().equals(btnCancel)){
				cancel();
			}else if(event.getSource().equals(btnAddDrOrder)){
				addDrOrders();
			}
			
		}
		
	}

}

